package com.zzh.partnersys.utils;

import cn.hutool.json.JSONObject;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import java.io.IOException;
import java.util.Map;

/**
 * 高德地图工具类
 */
@Component
public class GeoUtils {

    private static final String API_KEY = "66f0747300645759892c9af3a339b955";

    private static final String GEO_API_URL = "https://restapi.amap.com/v3/ip?key=" + API_KEY + "&ip=";

    private static final String GEO_API_ADDRESS_URL = "https://restapi.amap.com/v3/geocode/regeo?key=%s&location=%s,%s";

    private final static Gson gson = new Gson();

    private CloseableHttpClient httpClient;

    //提前加载，减少请求延�?
    @PostConstruct
    public void init() {
        httpClient = HttpClients.createDefault();
    }

    // 关闭HttpClient
    @PreDestroy
    public void destroy() {
        try {
            httpClient.close();
        } catch (IOException e) {
            System.out.println("关闭HttpClient出错: " + e.getMessage());
        }
    }

    /**
     * 获取当前IP的地理位置信息
     * @return 地理位置信息 "21.123456,120.123456"
     */
    public String getCurrentIPGeoInfo(String ip) {
        // 替换为你的高德地图API Key
        ip = "120.197.59.5";
        String url = String.format("%s%s",GEO_API_URL,ip);
//        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        String result = "";
//        try {
//            CloseableHttpResponse response = httpClient.execute(httpGet);
//            int statusCode = response.getStatusLine().getStatusCode();
//            if (statusCode == 200) {
//                HttpEntity entity = response.getEntity();
//                result = EntityUtils.toString(entity);
//                Map<String, Object> map = gson.fromJson(result, Map.class);
//                String rectangle = (String) map.get("rectangle");
//                System.out.println("rectangle: " + rectangle);
//                System.out.println("响应内容: " + result);
//                if (rectangle!= null){
//                    result = rectangle.split(";")[0];
//                }else {
//                    result = "";
//                }
//            } else {
//                System.out.println("请求失败，状态码: " + statusCode);
//            }
//        } catch (IOException e) {
//            System.out.println("发生错误: " + e.getMessage());
//        }
        return result;
    }

    // 根据经纬�?获取详细地址
    public String getAddress(String lng, String lat) {
        // 替换为你的高德地图API Key
        String urlAddress = String.format(GEO_API_ADDRESS_URL, API_KEY, lng, lat);
        HttpGet httpGet = new HttpGet(urlAddress);
        String formattedAddress = "";
        try {
            CloseableHttpResponse response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("响应状态码: " + statusCode);
            String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
            JSONObject jsonObject = new JSONObject(responseBody);
//            System.out.println(responseBody);
            if (statusCode == 200) {
                JSONObject regeocode = jsonObject.getJSONObject("regeocode");
                formattedAddress = regeocode.getStr("formatted_address");
            }
            return formattedAddress;
        } catch (IOException e) {
            System.out.println("发生错误: " + e.getMessage());
        }
        return formattedAddress;
    }



}
